"""
Case Type   : 基础功能
Case Name   : 创建递增序列，不指定最大值，指定large参数
Create At   : 2023/05/09
Owner       : cr13
Description :
    1.创建递增序列，不指定最大值，指定large参数
    2.循环查询序列值到2^127-1
    3.再次查询序列值
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.合理报错
    4.成功
History     :
"""
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.comshpri = CommonSH('PrimaryDbUser')
        self.sequence = "s_ddl_sequence_014"

    def test_create_sequence(self):
        text = '----step1:创建递增序列，不指定最大值，指定large参数 ' \
               'expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop large sequence if exists {self.sequence};" \
            f"create large sequence {self.sequence};"
        result = self.comshpri.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('DROP LARGE SEQUENCE', result, '执行失败' + text)
        self.assertIn('CREATE LARGE SEQUENCE', result, '执行失败' + text)

        text = '----step2:循环查询序列值到2^127-1 expect:成功----'
        self.log.info(text)
        sql_cmd = f"select setval('{self.sequence}',170141183460469231731687303715884105727);"
        result = self.comshpri.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertNotIn('ERROR', result, '执行失败' + text)

        text = '----step3:再次查询序列值 expect:合理报错----'
        self.log.info(text)
        sql_cmd = f"select nextval('{self.sequence}');"
        result = self.comshpri.execut_db_sql(sql_cmd)
        self.log.info(result)
        # self.assertIn('ERROR', result, '执行失败' + text)

    def tearDown(self):
        text = "----step4:环境清理 expect:成功----"
        self.log.info(text)
        result = self.comshpri.execut_db_sql(
            f"drop large sequence if exists {self.sequence};")
        self.log.info(result)
        self.assertIn('DROP LARGE SEQUENCE', result, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
